package com.wing.sys.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wing.common.Constants;
import com.wing.common.util.PageBean;
import com.wing.sys.domain.Module;
import com.wing.sys.service.ModuleManager;

/**
 * 
 * 
 * @author please  write your big name
 * 
 */
 
@Controller
@RequestMapping(value = "/sys/module")
public class ModuleController {

	@Autowired
	private ModuleManager moduleManager;
	
	/**
	 * 获得列表数据
	 *  
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String list() {
		return "/sys/module/moduleIndex";
	}
	
	@RequestMapping(value = "get/{url}", method = RequestMethod.GET)
	public String get(@PathVariable String url) {
		return "sys/module/"+url;
	}
	
	/**
	 * 保存一个模块
	 * @param org
	 * @return
	 */
	@RequestMapping(value = "save", method = RequestMethod.POST)
	public @ResponseBody String save(Module m,HttpServletRequest request) {
		String ptree_id = request.getParameter("ptree_id");
		moduleManager.saveModule(m,ptree_id);
		return Constants.DEFAULT_AJAX_SUCCESS;
	}

	/**
	 * 打开模块编辑页面
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "toEdit/{id}", method = RequestMethod.GET)
	public String toEditPage(@PathVariable
	String id, Model model,HttpServletRequest request) {
		Module m = new Module();
		Module pm = null;
		if(!id.equals("0")){
			m = moduleManager.findById(id);
			pm = moduleManager.findById(m.getParent_id());
		}else{
			String pmoduleId = request.getParameter("pmoduleId");
			m.setParent_id(pmoduleId);
			pm = moduleManager.findById(pmoduleId);
		}
		model.addAttribute("data", m);
		model.addAttribute("ismenu", m.getIsmenu());
		model.addAttribute("ispub", m.getIspub());
		model.addAttribute("linkType", m.getLink_type());
		model.addAttribute("pdata", pm);
		return "sys/module/moduleForm";
	}
	

	
	/**
	 * 删除组织机构
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "delmodules", method = RequestMethod.POST)
	public @ResponseBody String delmodules(HttpServletRequest request) throws IOException {
		String chk [] = request.getParameterValues("chk");
		int count = moduleManager.deleteModules(chk);
		return ""+count;
	}
	
	/**
	 * ajax方式 获取一个组织机构列表 
	 * @return
	 */
	@RequestMapping(value = "list", method = RequestMethod.POST)
	public @ResponseBody PageBean list(int currentPage,
			String pageSize,String moduleId) {
		int _iPageSize = 0;
		if (pageSize == null) {
			_iPageSize = PageBean.DEFAULT_PAGESIZE;
		} else {
			_iPageSize= Integer.parseInt(pageSize);
		}
		return  moduleManager.findPageList(currentPage, _iPageSize,moduleId);
	}
	
	/**
	 * 是根据组织机构parentId获取的子机构,生成树
	 * 
	 * @param request
	 * @param response
	 * @param id
	 * @return
	 *  
	 * @throws ServletException, IOException
	 */
	@RequestMapping(value = "showTree", method = RequestMethod.GET)
	public void showTree(HttpServletRequest request,
			HttpServletResponse response) {
		response.setContentType("text/xml;charset=UTF-8");
		response.setHeader("Pragma", "No-cache");//HTTP 1.1    
		response.setHeader("Cache-Control", "no-cache");//HTTP 1.0    
		response.setHeader("Expires", "0");//防止被proxy
		
		String userId = (String)request.getSession().getAttribute(Constants.SESSION_USER_ID);
		String pid = request.getParameter("id");
		String menu = request.getParameter("menu");
		boolean firstFlag = false;
		if (pid == null || pid.equalsIgnoreCase("")) 
			pid = Module.ROOT_MODULE;//我数据库中一级节点的parentid都是-1
			firstFlag = true;
		try {
			//根据上一级节点,查询该节点下的所有节点 
//			String aa = moduleManager.createDHTMLXML(pid,firstFlag);
			if("true".equals(menu)){
				response.getWriter().write(moduleManager.createDHTMLXML(pid,firstFlag,true,userId));
			}else{
				response.getWriter().write(moduleManager.createDHTMLXML(pid,firstFlag,false));
			}
			response.getWriter().close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
}

